#include "SavingsAccount.hh"
#include <fstream>
#include <iomanip>
#include <iostream>

SavingsAccount::SavingsAccount(const std::string& id, const std::string& owner, double initialBalance)
    : BankAccount(id, owner, initialBalance) {}

bool SavingsAccount::deposit(double amount, const std::string& note) {
    std::cout << "Error: Deposits not allowed for Savings Account.\n";
    return false;
}

bool SavingsAccount::withdraw(double amount, const std::string& note) {
    std::cout << "Error: Withdrawals not allowed for Savings Account.\n";
    return false;
}

bool SavingsAccount::transferTo(BankAccount* destination, double amount, const std::string& note) {
    if (!destination) {
        std::cout << "Error: Invalid destination account.\n";
        return false;
    }
    if (amount <= 0 || amount > balance || amount > 1000000.0) {
        std::cout << "Error: Invalid transfer amount (must be positive and less than 1,000,000).\n";
        return false;
    }
    if (destination->getOwnerName() != ownerName || destination->getAccountType() != "Checking") {
        std::cout << "Error: Savings can only transfer to same customer's Checking Account.\n";
        return false;
    }

    balance -= amount;
    destination->balance += amount;

    Transaction t("T" + std::to_string(Transaction::getNextId()), amount, "Transfer Out", note);
    addTransaction(t);
    destination->addTransaction(Transaction("T" + std::to_string(Transaction::getNextId()), amount, "Transfer In", note));

    std::cout << "Transfer successful! New balance: " << std::fixed << std::setprecision(2) << balance << "\n";
    return true;
}